package com.wrial.middle;
/*
 * @Author  Wrial
 * @Date Created in 13:12 2020/3/24
 * @Description  2
 */

import com.sun.org.apache.xpath.internal.operations.String;

public class LeetCode_02 {

    public class ListNode {
        ListNode(int x) {
            val = x;
        }

        int val;

        ListNode next;
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

        ListNode myNodes = new ListNode(-1);
        ListNode p = myNodes;
        int flag = 0;

        while (l1 != null || l2 != null) {

            int v1 = 0;
            int v2 = 0;

            if (l1 != null) {
                v1 = l1.val;
                l1 = l1.next;
            }
            if (l2 != null) {
                v2 = l2.val;
                l2 = l2.next;
            }
            int temp = v1 + v2 + flag;
            flag = temp / 10;
            ListNode newNode = new ListNode(temp % 10);
            p.next = newNode;
            p = p.next;
        }

        if (flag>0){
            ListNode newNode = new ListNode(flag);
            p.next = newNode;
        }

        return myNodes.next;
    }

    public static void main(String[] args) {

    }
}
